# -*- coding:utf-8 -*-

from typing import List
from sqlalchemy.ext.asyncio import AsyncSession

from exceptions.exception import ServiceException
from utils.common_util import CamelCaseUtil, export_list2excel
from module_admin.entity.vo.sys_table_vo import SysTablePageModel
from module_admin.service.sys_table_service import SysTableService
from utils.page_util import PageResponseModel
from module_vip.dao.bg_vip_dao import BgVipDao
from module_vip.entity.do.bg_vip_do import BgVip
from module_vip.entity.vo.bg_vip_vo import BgVipPageModel, BgVipModel


class BgVipService:
    """
    用户管理模块服务层
    """

    @classmethod
    async def get_bg_vip_list(cls, query_db: AsyncSession, query_object: BgVipPageModel, data_scope_sql: str) -> [list | PageResponseModel]:
        bg_vip_list = await BgVipDao.get_bg_vip_list(query_db, query_object, data_scope_sql, is_page=True)
        return bg_vip_list

    @classmethod
    async def get_bg_vip_by_id(cls, query_db: AsyncSession, bg_vip_id: int) -> BgVipModel:
        bg_vip = await  BgVipDao.get_by_id(query_db, bg_vip_id)
        bg_vip_model = BgVipModel(**CamelCaseUtil.transform_result(bg_vip))
        return bg_vip_model

    @classmethod
    async def get_bg_vip_by_user_id(cls, query_db: AsyncSession, user_id: int) -> BgVipModel:
        bg_vip = await  BgVipDao.get_by_user_id(query_db, user_id)
        if bg_vip:
            bg_vip_model = BgVipModel(**CamelCaseUtil.transform_result(bg_vip))
            return bg_vip_model
        else:
            raise ServiceException(message="vip不存在")

    @classmethod
    async def add_bg_vip(cls, query_db: AsyncSession, query_object: BgVipModel) -> BgVipModel:
        bg_vip_model = await BgVipDao.add_bg_vip(query_db, query_object)
        return bg_vip_model


    @classmethod
    async def update_bg_vip(cls, query_db: AsyncSession, query_object: BgVipModel) -> BgVipModel:
        bg_vip = await BgVipDao.edit_bg_vip(query_db, query_object)
        bg_vip_model = BgVipModel(**CamelCaseUtil.transform_result(bg_vip))
        return bg_vip_model


    @classmethod
    async def del_bg_vip(cls, query_db: AsyncSession, bg_vip_ids: List[str]):
        await BgVipDao.del_bg_vip(query_db, bg_vip_ids)


    @classmethod
    async def export_bg_vip_list(cls, query_db: AsyncSession, query_object: BgVipPageModel, data_scope_sql) -> bytes:
        bg_vip_list = await BgVipDao.get_bg_vip_list(query_db, query_object, data_scope_sql, is_page=False)
        filed_list = await SysTableService.get_sys_table_list(query_db, SysTablePageModel(tableName='bg_vip'), is_page=False)
        filtered_filed = sorted(filter(lambda x: x["show"] == '1', filed_list), key=lambda x: x["sequence"])
        new_data = []
        for item in bg_vip_list:
            mapping_dict = {}
            for fild in filtered_filed:
                if fild["prop"] in item:
                    mapping_dict[fild["label"]] = item[fild["prop"]]
            new_data.append(mapping_dict)
        binary_data = export_list2excel(new_data)
        return binary_data

    @classmethod
    async def get_vip_statistics(cls, query_db: AsyncSession) -> dict:
        """
        获取VIP会员统计信息
        """
        statistics = await BgVipDao.get_vip_statistics(query_db)
        return statistics